package Prefix_and;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-01-28
 * Time:19:43
 * Text：1314. 矩阵区域和
 */
public class Text8 {

    public static void main(String[] args) {
        int[][] mat={{67,64,78},{99,98,38},{82,46,46},{6,52,55},{55,99,45}};
        int m=3;
        int[][] dp=new int[mat.length+1][mat[0].length+1];


        for(int i=1;i<=mat.length;i++){
            for(int k=1;k<=mat[0].length;k++){
                dp[i][k]=dp[i][k-1]+dp[i-1][k]-dp[i-1][k-1]+mat[i-1][k-1];
            }
        }
        int[][] dp2=new int[mat.length][mat[0].length];
        for(int i=0;i<mat.length;i++){
            for(int k=0;k<mat[0].length;k++){
                int x1=Math.max(0,i-m)+1;  int y1=Math.max(0,k-m)+1;
                int x2=Math.min(mat.length-1,i+m)+1;int y2=Math.min(mat[0].length-1,k+m)+1;
                dp2[i][k]=dp[x2][y2]-dp[x1-1][y2]-dp[x2][y1-1]+dp[x1-1][y1-1];
            }
        }
    }
}
